package driver.ui.driver;

import java.util.ArrayList;



import driver.ui.R;
import driver.ui.driver.Beans.notification;
import driver.ui.driver.Beans.pickupsetObject;
import Databasehelper.Notificationhelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;


public class Pickupdetail extends Activity{
	ListView detailList;
	TextView userName;
	PickupsetAdapter arrayAdapter;
	pickupsetObject set5;
	pickupsetObject set4;
	pickupsetObject set;
	pickupsetObject set1;
	pickupsetObject set2;
	pickupsetObject set3;
	String username;
	String sentfrom;
	String sentto;
	String s="";
	Boolean flagstatus;
	Notificationhelper dbhelper;
    static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.pickupdetail);
	        detailList = (ListView) findViewById(R.id.detailList);
	        userName = (TextView) findViewById(R.id.pickupto);
	        ArrayList<pickupsetObject>  picklist= new ArrayList<pickupsetObject>();
	        flagstatus=false;
	        dbhelper=new Notificationhelper(this);
        
	        //Initialize our array adapter notice how it references the listitems.xml layout
	        arrayAdapter = new PickupsetAdapter(Pickupdetail.this, R.layout.listitems,picklist);

	        //This Page is built based on ListView
	        final AlertDialog.Builder shareBuilder=new AlertDialog.Builder(this);

	      //Get Detail Notification Info
	        Bundle extras = getIntent().getExtras();
				
			sentto= extras.getString("to");
			sentfrom= extras.getString("from");
			username=extras.getString("username");
			userName.setText(sentto);
			ArrayList<notification> nots=dbhelper.getAllNotByTo(sentto);
			String date="";
			String time="";
			String sharing="";
			for(int i=0;i<nots.size();i++)
			{
				notification not;
				not=nots.get(i);
				date=not.getDate();
				time=not.getTime();
				sharing=not.getShare();
				
			}
			

	        set = new pickupsetObject();
	        set.setOption("Date");
	        set.setValue(date);
	        picklist.add(set);
	        
	       set2 = new pickupsetObject();
	        set2.setOption("Time");
	        set2.setValue(time);
	       	picklist.add(set2);
	       	
	      
	       	
	       	set3 = new pickupsetObject();
	        set3.setOption(sentfrom+"'s Sharing");
	        set3.setValue(sharing);
	       	picklist.add(set3);
	       	
	       	
	       	set4 = new pickupsetObject();
	        set4.setOption("Your Sharing");
	       	picklist.add(set4);
	       	
	       	
	        //Set the above adapter as the adapter of choice for our list
	        detailList.setAdapter(arrayAdapter);
	       

	        //ListView's Listener
	        detailList.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long arg3) {
					switch(position)
					{
				
					//This is for "Sharing"
					case 3:
						final CharSequence[] StatusTime = {"Your Location", "Your Status"};
						
						shareBuilder.setCancelable(true);
						shareBuilder.setIcon(R.drawable.w);
						shareBuilder.setTitle("When you near the pickup point, share the following info:");
						shareBuilder.setSingleChoiceItems(StatusTime, 0, new DialogInterface.OnClickListener ()
						{
							@Override

						    public void onClick(DialogInterface dialog, int id) {
							}
					

						});
					    shareBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
	
				            	   if(!flagstatus)
				            	   {			            	  
				            		   if(s.equals(""))
				            			   s=s+"location";
				            		   else
				            			   s=s+",location";
				            		   flagstatus=true;
				            	   }
				            	   else
				            	   {
				            		   
				            		   if(s.equals(""))
				            			   s=s+"Status";
				            		   else
				            			   s=s+",Status";
				            	   }
				            				   
				            		   
				            	   
				            	   set4.setValue(s);
				            	   
				            	   arrayAdapter.notifyDataSetChanged();
				           
				               }
				           })
				           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int id) {
				                    dialog.cancel();
				               }
				           });
					
						shareBuilder.show();
						break;
					
					}
					
				
					
				}});
	 }

	    public void confirmNotificationsClicked(View view)
	    {
			
			notification not=new notification();
			not.setSentfrom(sentto);
			String[] usernames=sentfrom.split(" ");
			not.setSentto(usernames[0]);
			not.setShare(set4.getValue());
			not.setType("confirmation");
			dbhelper.insertConfirm(not);
			Intent intent=new Intent();
			intent.putExtra("name",userName.getText().toString());
			intent.setAction("Contactlist");
			startActivity(intent);
	    }
	 
	
	 
	    }
	 
	       
	 
	 


